package si.unimb.src.wifisms;

import java.util.ArrayList;

import si.unimb.src.wifisms.control.Database;
import si.unimb.src.wifisms.entity.Account;
import si.unimb.src.wifisms.ui.AccountArrayAdapter;
import si.unimb.src.wifisms.util.Utility;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class Accounts extends Activity implements OnItemClickListener, OnClickListener {

	private ListView listItems;
	private Button noviRacun;
	private ArrayList<Account> accounts;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accounts);
        
        noviRacun = (Button)findViewById(R.id.NovRacunButton);
        noviRacun.setOnClickListener(this);
        
        accounts = Database.getAccounts(this);
        
        this.listItems = (ListView)findViewById(R.id.RacuniListView);
        this.listItems.setOnItemClickListener(this);
        registerForContextMenu(this.listItems);
        
	}
	
	@Override
	protected void onResume() {
		
		// Parent
		super.onResume();
		
		// Osvezevanje vsebine
		try {
	        AccountArrayAdapter aaa = new AccountArrayAdapter(this, android.R.layout.simple_list_item_2, accounts);
	        this.listItems.setAdapter(aaa);
        } catch (Exception e) {
        	System.out.println(e.getMessage());
        }
		
	}

	@Override
	public void onClick(View v) {
		if(v.equals(noviRacun)) // novi račun
			Utility.gotoIntent(this, EditAccount.class, Utility.NO_EXTRAS);		
	}
	
	/**
	 * urejanje računa, ko kliknemo na enega iz med elementov
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int id, long arg3) {
		Utility.gotoIntent(this, EditAccount.class, id); // urejanje računa
	}
	
	/**
	 * vsebina context menija
	 */
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	    menu.setHeaderTitle(accounts.get(info.position).getDescription()); /* substrings(maxDolžina)...? */
	    	    
	    String[] contextMenuItems = getResources().getStringArray(R.array.racunContextMenuItems);
	    for (int i = 0; i < contextMenuItems.length; i++) {    	
	    	menu.add(Menu.NONE, i, i, contextMenuItems[i]);
	    }
    }
	
	/**
	 * kaj se proži ob izbiri elementa context menija
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();

		int id = menuInfo.position;
		if(item.getItemId() == 0) {
			Utility.gotoIntent(this, EditAccount.class, id);
		} else if (item.getItemId() == 1) { //izbris
			Database.getAccounts(this).remove(id);
			Database.persist(this);
			listItems.invalidateViews(); // refreshing...
		} else if (item.getItemId() == 2) {
			Database.setDefaultAccount(Database.getAccounts(this).get(id), this); // set as default
		}
		
		return false;
	}
	
}